package com.xiaohutech.tm.home.ui.vm

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.GsonUtils
import com.google.gson.reflect.TypeToken
import com.xiaohutech.tm.home.data.bean.GoodsListBean
import com.xiaohutech.tm.home.utils.GetJsonDataUtil
import com.yxh.lib_base.appContext
import com.yxh.lib_base.base.viewmodel.BaseViewModel
import com.yxh.lib_base.ext.lazyNone
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * @author yuanxh
 * @date :2024/2/7 22:39
 * @description:
 */
class HomeViewModule : BaseViewModel() {

    private val utils by lazyNone { GetJsonDataUtil() }

    val data = MutableLiveData<List<GoodsListBean>>()

    fun loadData() {
        viewModelScope.launch {
            data.value = loadTestData()
        }
    }

    private suspend fun loadTestData(): List<GoodsListBean> = withContext(Dispatchers.IO) {
        val json = utils.getJson(appContext, "home/test_product_list.json")
        GsonUtils.fromJson(json, object : TypeToken<List<GoodsListBean>>() {}.type)
    }
}